home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / locale-gen < prev    next >
Encoding:
Text File  |  2007-03-05  |  6.2 KB  |  240 lines

  1. #!/bin/sh
  2.  
  3. set -e
  4.  
  5. SUPPORTED=/var/lib/locales/supported.d
  6. LOCALES=/usr/share/i18n/locales
  7. STATEDIR=/var/lib/belocs
  8.  
  9. [ -n "$POSIXLY_CORRECT" ] && unset POSIXLY_CORRECT || true
  10.  
  11. ARCHIVE=no
  12. PURGE=no
  13. ALIASES=
  14. GLIBC_MAGIC=auto
  15. [ -f /etc/belocs/locale-gen.conf ] && . /etc/belocs/locale-gen.conf || true
  16.  
  17. #  Handle command-line options
  18. prev=
  19. while true
  20. do
  21.     option="$1"
  22.     if [ -n "$prev" ]; then
  23.                 eval "$prev=\$option"
  24.                 prev=
  25.                 continue
  26.     fi
  27.  
  28.     case $option in
  29.     -h|--help)
  30.         cat <<EOT
  31. Usage: locale-gen [OPTIONS]
  32. Options:
  33.  -h, --help         display this message and exit
  34.      --purge        remove existing locales before processing
  35.      --archive      store compiled locale data inside a single archive
  36.      --no-archive   do not store compiled locale data inside a single archive
  37.                     (default)
  38.      --aliases=FILE read locale aliases from FILE. (Default: /etc/locale.alias)
  39. EOT
  40.         exit 0
  41.         ;;
  42.     --purge)
  43.         PURGE=yes
  44.         ;;
  45.     --archive)
  46.         ARCHIVE=yes
  47.         ;;
  48.     --no-archive)
  49.         ARCHIVE=no
  50.         ;;
  51.     --keep-existing)
  52.         #  Cache in belocs-locales-bin makes this flag useless,
  53.         #  but it is provided so that belocs-locales-bin can be
  54.         #  installed on Ubuntu boxes.
  55.         ;;
  56.     --aliases)
  57.         prev=ALIASES
  58.         ;;
  59.     --aliases=*)
  60.         ALIASES=$(expr "x$option" : 'x[^=]*=\(.*\)')
  61.         ;;
  62.     --*)
  63.         echo "locale-gen: invalid option -- $option"
  64.         echo "Try 'locale-gen --help' for more information."
  65.         exit 1
  66.         ;;
  67.     *)
  68.         break
  69.         ;;
  70.     esac
  71.  
  72.     shift
  73. done
  74.  
  75. mkdir -p "$SUPPORTED"
  76.  
  77. # determine the locales to be generated in $GENERATE
  78. GENERATE=
  79. if [ -z "$1" ]; then
  80.     [ -n "`ls $SUPPORTED`" ] || exit 0
  81.     GENERATE=`cat $SUPPORTED/*`
  82. fi
  83.  
  84. while [ -n "$1" ]; do
  85.     if [ -f "$SUPPORTED/$1" ]; then
  86.         GENERATE="$GENERATE\n`cat $SUPPORTED/$1`"
  87.     elif L=`grep "^$1 " /usr/share/i18n/SUPPORTED`; then
  88.         GENERATE="$GENERATE\n$L"
  89.         # add to supported.d/local if necessary
  90.         if [ ! -f "$SUPPORTED/local" ] || ! grep -q "^$L" "$SUPPORTED/local"; then
  91.             echo "$L" >> "$SUPPORTED/local"
  92.         fi
  93.     else
  94.         echo "Error: '$1' is not a supported language or locale" >&2
  95.         exit 1
  96.     fi
  97.     shift
  98. done
  99.  
  100. [ -d "$STATEDIR" ] || mkdir -p "$STATEDIR" || PURGE=yes
  101.  
  102. no_archive=
  103. [ "$ARCHIVE" = yes ] || no_archive="--no-archive"
  104. locale_alias=
  105. [ -n "$ALIASES" ] && [ -r "$ALIASES" ] && locale_alias="-A $ALIASES"
  106. if [ "$GLIBC_MAGIC" = auto ]
  107. then
  108.     GLIBC_MAGIC=20051014
  109. fi
  110. case "$GLIBC_MAGIC" in
  111.     "20051014" | "20031115" | "20000828" )
  112.         ;;
  113.     * )
  114.         echo "Invalid magic number: $GLIBC_MAGIC" 1>&2
  115.         exit 3
  116.         ;;
  117. esac
  118.  
  119. umask 022
  120.  
  121. is_entry_ok() {
  122.     if [ -n "$locale" ] && [ -n "$charset" ]
  123.     then
  124.         true
  125.     else
  126.         echo "Error: Bad entry '$locale $charset'" 1>&2
  127.         false
  128.     fi
  129. }
  130.  
  131. normalize_locale() {
  132.     #  Insert a leading x in case $1 begins with a dash
  133.     this_locale=x$1
  134.     charset=
  135.     if echo $this_locale | LC_ALL=C grep '\.' > /dev/null 2>&1; then
  136.         charset=$(echo $this_locale | sed -e 's/^x//' -e 's/.*\(\.[^@]*\).*/\1/' | LC_ALL=C tr '[A-Z]' '[a-z]' | LC_ALL=C sed -e 's/[^a-z0-9.]//g')
  137.     fi
  138.     modifier=
  139.     if echo $this_locale | LC_ALL=C grep '@' > /dev/null 2>&1; then
  140.         modifier=$(echo $this_locale | sed -e 's/^x//' -e 's/.*\(@[^.]*\).*/\1/')
  141.     fi
  142.     main=$(echo $this_locale | sed -e 's/^x//' -e 's/[.@].*//')
  143.     echo $main$charset$modifier
  144. }
  145.  
  146. gen_dep() {
  147.     [ -d "$STATEDIR" ] || return 1
  148.     #  Generate the dependency list.  It is needed by update_md5sum()
  149.     ( LC_ALL=C grep '^\(include\|copy\)' "$LOCALES"/* |\
  150.       sed -e "s,^$LOCALES/,," -e 's/\(include\|copy\)[     ]*"/ /' -e 's/".*//'
  151.     ) | LC_ALL=C sort -u > "$STATEDIR/locales.dep" 2>/dev/null
  152. }
  153.  
  154. gen_md5sum() {
  155.     [ -d "$STATEDIR" ] || return 1
  156.     #  If old magic number is not stored or differs from current
  157.     #  one, regenerate all locales.
  158.     [ -f "$STATEDIR/magic" ] || return 1
  159.     old_magic=$(sed -e 1q "$STATEDIR/magic")
  160.     [ "$old_magic" = "$GLIBC_MAGIC" ] || return 1
  161.  
  162.     #  If old md5sums are not present, regenerate all locales.
  163.     [ -f "$STATEDIR/hashfile" ] || return 1
  164.     mv -f "$STATEDIR/hashfile" "$STATEDIR/hashfile.old" || return 1
  165.  
  166.     md5sum "$LOCALES"/* > "$STATEDIR/hashfile.new" || return 1
  167.     locale -a > "$STATEDIR/list" 2>/dev/null || return 1
  168. }
  169.  
  170. gen_magic ()
  171. {
  172.     echo "$GLIBC_MAGIC" > "$STATEDIR/magic" || true
  173.     :> "$STATEDIR/hashfile" || true
  174. }
  175.  
  176. #  Check whether a locale was already generated and all its dependencies
  177. #  have not been modified.
  178. already_generated() {
  179.     [ -f "$STATEDIR/locales.dep" ] || return 1
  180.     [ -f "$STATEDIR/hashfile.old" ] || return 1
  181.     [ -f "$STATEDIR/hashfile.new" ] || return 1
  182.     [ -f "$STATEDIR/list" ] || return 1
  183.     this_locale=$(normalize_locale $1)
  184.     this_input=$2
  185.     #  Check whether the locale is already present.
  186.     LC_ALL=C grep "^$this_locale\$" "$STATEDIR/list" > /dev/null 2>&1 || return 1
  187.     #  If yes, check if this file and its dependencies have changed
  188.     for file in "$this_input" $(LC_ALL=C grep "^$this_input:" "$STATEDIR/locales.dep" | awk '{printf "%s ", $2}')
  189.     do
  190.         md5old=$(LC_ALL=C grep "/$file\$" "$STATEDIR/hashfile.old" | sed -e 1q)
  191.         md5new=$(LC_ALL=C grep "/$file\$" "$STATEDIR/hashfile.new" | sed -e 1q)
  192.         [ -n "$md5old" ] || return 1
  193.         [ -n "$md5new" ] || return 1
  194.         [ "$md5new" = "$md5old" ] || return 1
  195.     done
  196.     return 0
  197. }
  198.  
  199. update_md5sum() {
  200.     [ -f "$STATEDIR/locales.dep" ] || return 0
  201.     this_locale=$1
  202.     for file in "$this_locale" $(LC_ALL=C grep "^$this_locale:" "$STATEDIR/locales.dep" | awk '{printf "%s ", $2}')
  203.     do
  204.         md5sum "$LOCALES/$file" >> "$STATEDIR/hashfile" || return 0
  205.     done
  206. }
  207.  
  208. gen_dep 2>/dev/null || true
  209. [ "$PURGE" = yes ] || gen_md5sum 2>/dev/null || PURGE=yes
  210. if [ "$PURGE" = yes ]; then
  211.     rm -f "$STATEDIR/hashfile.old" "$STATEDIR/hashfile.new" "$STATEDIR/list" 2>/dev/null || true
  212.     rm -rf /usr/lib/locale/* 2>/dev/null || true
  213. fi
  214. gen_magic 2>/dev/null || true
  215.  
  216. echo "Generating locales..."
  217. /bin/echo -e "$GENERATE" | sort -u | while read locale charset
  218. do
  219.     case $locale in
  220.         \#*) continue;;
  221.         "") continue;;
  222.     esac
  223.     is_entry_ok || continue
  224.     #  Remove charset if present
  225.     input=$(echo $locale | sed -e 's/\.[^@]*//')
  226.     #  Insert charset before modifier
  227.     printf "%s" $input | sed -e "s/^\\([^@]*\\)\\(.*\\)/  \\1.${charset}\\2... /"
  228.     if already_generated $locale $input; then
  229.         echo 'up-to-date'
  230.     else
  231.         if localedef $no_archive --magic=$GLIBC_MAGIC -i $input -c -f $charset $locale_alias $locale; then    
  232.             echo 'done'
  233.         else
  234.             echo 'failed'
  235.         fi
  236.     fi
  237.     update_md5sum $input 2>/dev/null
  238. done
  239. echo "Generation complete."
  240.